package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.dao.MenuDao;
import com.litong.litongjiaoyu.entity.SysPosition;
import com.litong.litongjiaoyu.service.PositionService;
import com.litong.litongjiaoyu.utils.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PositionController
 * @Description 职位管理控制层
 * @Author jingz
 * @Date 2019/10/16 20:08
 * @Version 1.0
 **/

@Log4j2
@RestController
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionService positionService;

    @Autowired
    private MenuDao menuDao;

    /**
     * 获取所有功能内容
     *
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @GetMapping("/queryfeatrues")
    public JSONResult queryFeatrues() {
        return JSONResult.ok(menuDao.query());
    }


    @IgnoreAuth
    @ResponseBody
    @PostMapping("/getablefeaturesid")
    public JSONResult getAbleFeaturesId(int position_id) {
        return JSONResult.ok(menuDao.getAbleFeaturesId(position_id));
    }

    /**
     * 查询表中所有信息
     *
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public JSONResult query() {
        List<SysPosition> positions = positionService.query();
        return JSONResult.ok(positions);
    }

    /**
     * 查询表中职位名称
     *
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @RequestMapping(value = "/queryname", method = RequestMethod.GET)
    public JSONResult queryName() {
        List<String> positions = positionService.queryName();
        return JSONResult.ok(positions);
    }

    /**
     * 根据id查询相应信息
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public JSONResult select(long id) {
        return JSONResult.ok(menuDao.getAbleFeatures(id));
    }

    /**
     * 添加职位信息
     *
     * @param name
     * @param features_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JSONResult insert(String name, long[] features_id) {
        SysPosition position = new SysPosition();
        position.setPosition(name);

        if (positionService.insert(position) != 1) {
            return JSONResult.errorMsg("fail");
        } else {
            position = positionService.getId(name);

            if (menuDao.add(position.getId(), features_id) > 0) {
                return JSONResult.ok("success");
            } else {
                positionService.remove((int) position.getId());
                return JSONResult.errorMsg("fail");
            }
        }
    }

    /**
     * 修改职位信息
     *
     * @param sysPosition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONResult update(@RequestBody SysPosition sysPosition, long[] features_id) {
        if (menuDao.remove(sysPosition.getId()) >= 0) {
            if (positionService.update(sysPosition) >= 0) {
                if (menuDao.add(sysPosition.getId(), features_id) > 0) {
                    return JSONResult.ok("success");
                } else {
                    return JSONResult.errorMsg("fail3");
                }
            } else {
                return JSONResult.errorMsg("fail2");
            }
        } else {
            return JSONResult.errorMsg("fail1");
        }
    }

    /**
     * 删除单个信息
     *
     * @param id
     * @return
     */
    @PostMapping("/remove")
    public JSONResult remove(long id) {
        if (positionService.remove(id) > 0) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/batchremove")
    public JSONResult batchRemove(long[] ids) {

        if (positionService.batchremove(ids) > 0) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }
}
